package com.vnas.fonan;

import java.util.ArrayList;
import java.util.List;

import android.R.color;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.renderscript.Type;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.TextSwitcher;
import android.widget.TextView;
import android.widget.ViewSwitcher.ViewFactory;

import com.vnas.adapter.PagerAdapter;
import com.vnas.fragment.FragmentExpense;
import com.vnas.fragment.FragmentOverView;
import com.vnas.fragment.MyFragment;

public class MainActivity extends FragmentActivity implements
		OnPageChangeListener {

	private List<MyFragment> fList;
	private ViewPager viewPager;
	private PagerAdapter myApdaper;
	private int curFragIndex = -1;
	private TextSwitcher tsTitle;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		fList = new ArrayList<MyFragment>();
		initViews();

		importFragment();
		if (fList != null && fList.size() > 0) {
			myApdaper = new PagerAdapter(getSupportFragmentManager(), fList);
			viewPager.setAdapter(myApdaper);
		}

	}

	private void initViews() {
		viewPager = (ViewPager) findViewById(R.id.viewpager_main);
		viewPager.setOnPageChangeListener(this);
		tsTitle = (TextSwitcher) findViewById(R.id.ts_title);
		tsTitle.setFactory(new ViewFactory() {

			@Override
			public View makeView() {

				TextView v = new TextView(MainActivity.this);
				v.setText("Overview");
				v.setTextSize(28.0f);
				v.setTextColor(Color.BLACK);
				
				return v;
			}
		});
		tsTitle.setAnimation(AnimationUtils.loadAnimation(
				getApplicationContext(), android.R.anim.fade_in));
	}

	private void importFragment() {

		FragmentOverView fo = new FragmentOverView();
		String u = getIntent().getExtras().getString("username");
		fo.setUsername(u);
		fList.add(fo);
		fList.add(new FragmentExpense().getFragment());

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public void onPageScrollStateChanged(int pos) {

	}

	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {

	}

	@Override
	public void onPageSelected(int pos) {
		curFragIndex = pos;
		tsTitle.setText(fList.get(curFragIndex).getFragment().getTitle());
		

	}

}
